package com.doan.classcauhoi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class Quanlycauhoi extends SQLiteOpenHelper {

	private static String DB_PATH = "/data/data/com.doan.gameshow/databases/";
	private static String DB_NAME = "databasecauhoi.sqlite";
	private static final int DATABASE_VERSION=1;
	
	private static final String TABLE_NAME="tablecauhoi";
	private static final String KEY_ID="_id";
	private static final String KEY_CAUHOI="cauhoi";
	private static final String KEY_A="cau_a";
	private static final String KEY_B="cau_b";
	private static final String KEY_C="cau_c";
	private static final String KEY_D="cau_d";
	private static final String KEY_DA="dapan";

	  
	private SQLiteDatabase myDataBase;
	private final Context myContext;

	
	public Quanlycauhoi(Context context) {
		super(context, DB_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
		myContext=context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	public void openDataBase() throws SQLException{
		 
		//Open the database
		String myPath = DB_PATH + DB_NAME;
		myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
		 
		}
	 private boolean checkDataBase(){ 
		 SQLiteDatabase checkDB = null;
		  
		 try{
			 String myPath = DB_PATH + DB_NAME;
			 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
		 }catch(SQLiteException e){
			 //database chua ton tai
		 }
		 
		 if(checkDB != null)
			 checkDB.close();
		  
		 return checkDB != null ? true : false;
	}
	 private void copyDataBase() throws IOException{
		 
		 	//mo db trong thu muc assets nhu mot input stream
			InputStream myInput = myContext.getAssets().open(DB_NAME);
			 
			//duong dan den db se tao
			String outFileName = DB_PATH + DB_NAME;
			 
			//mo db giong nhu mot output stream
			OutputStream myOutput = new FileOutputStream(outFileName);
			 
			//truyen du lieu tu inputfile sang outputfile
			byte[] buffer = new byte[1024];
			int length;
			while ((length = myInput.read(buffer))>0)
			{
				myOutput.write(buffer, 0, length);
			}
			 
			//Dong luon
			myOutput.flush();
			myOutput.close();
			myInput.close();
			 
	}

	 public void createDataBase() throws IOException{
		 boolean dbExist = checkDataBase(); //kiem tra db
		  
		 if(dbExist){
		 	//khong lam gi ca, database da co roi
		 }
		 else{
			 this.getReadableDatabase();
			 try {
				 copyDataBase(); //chep du lieu
		 	} 
		 	catch (IOException e) {
		 		throw new Error("Error copying database");
		 	}
		 }
	 }  
	
	 public List<Cauhoi> layNcaungaunhien(int socau)
		{
			List<Cauhoi> ds_cauhoi=new ArrayList<Cauhoi>();
			String limit="0,"+socau;
			SQLiteDatabase db=this.getReadableDatabase();

			Cursor contro=db.query(TABLE_NAME, //ten bang
					null , //danh sach cot can lay
					null, //dieu kien where
					null, //doi so dieu kien where
					null, //bieu thuc groupby
					null, //bieu thuc having
					"random()", //bieu thuc orderby
					limit //"0,3" //bieuthuc limit
					);
		
			contro.moveToFirst();
			do{
				Cauhoi list=new Cauhoi();
				list._id=Integer.parseInt(contro.getString(0));
				list.cauhoi=contro.getString(1);
				list.cau_a=contro.getString(2);
				list.cau_b=contro.getString(3);			
				list.cau_c=contro.getString(4);			
				list.cau_d=contro.getString(5);
				list.dapan=contro.getString(6);
				list.image=contro.getBlob(7);
				list.imagea=contro.getBlob(8);
				list.imageb=contro.getBlob(9);
				list.imagec=contro.getBlob(10);
				list.imaged=contro.getBlob(11);
				list.flag=false;
				ds_cauhoi.add(list);
			}while(contro.moveToNext());
			
			return ds_cauhoi;
		}
	@Override
	public synchronized void close() {
		 
		if(myDataBase != null)
		myDataBase.close();
		 
		super.close();
		 
		}

	public Cursor laytatcacauhoi() {
		SQLiteDatabase db=this.getReadableDatabase();
		Cursor contro=db.rawQuery("select * from tablecauhoi", null);
		return contro;
	}


}
